<?php
declare (strict_types = 1);

namespace app\common\controller\upload;

use app\admin\controller\AdminController;
use app\admin\service\ConfigService;
use app\Request;
use think\facade\View;

/**
 * 上传管理
 *
 * @Author tanruizheng
 * @DateTime 2021-07-19
 */
class Upload extends AdminController
{

    /**
     * 上传配置
     *
     * @Author tanruizheng
     * @DateTime 2021-07-19
     * @return void
     */
    public function setting(Request $request){
        if($request->isPost()){
            $data = [
                'uploadmaxsize' => $request->param('uploadmaxsize'),//允许上传附件大小
                'uploadallowext' => $request->param('uploadallowext'),//许上传附件类型
                'qtuploadmaxsize' => $request->param('qtuploadmaxsize'),//前台允许上传附件大小
                'qtuploadallowext' => $request->param('qtuploadallowext'),//前台允许上传附件类型
                'watermarkenable' => $request->param('watermarkenable'),//是否开启图片水印
                'watermarkminwidth' => $request->param('watermarkminwidth'),//水印-宽
                'watermarkminheight' => $request->param('watermarkminheight'),//水印-高
                'watermarkimg' => $request->param('watermarkimg'),//水印图片
                'watermarkpct' => $request->param('watermarkpct'),//水印透明度
                'watermarkquality' => $request->param('watermarkquality'),//水印质量
                'watermarkpos' => $request->param('watermarkpos'),//水印位置
                'attachment_driver' => $request->param('attachment_driver'),//附件驱动
                'attachment_aliyun_key_id' => $request->param('attachment_aliyun_key_id'),//OSS-accessKeyId
                'attachment_aliyun_key_secret' => $request->param('attachment_aliyun_key_secret'),//OSS-accessKeySecret
                'attachment_aliyun_endpoint' => $request->param('attachment_aliyun_endpoint'),//attachment_aliyun_endpoint
                'attachment_aliyun_bucket' => $request->param('attachment_aliyun_bucket'),//OSS-bucket
            ];
            $res= ConfigService::updateConfig($data);
            return json($res);
        }
        $fields = [
            'uploadmaxsize',
            'uploadallowext',
            'qtuploadmaxsize',
            'qtuploadallowext',
            'watermarkenable',
            'watermarkminwidth',
            'watermarkminheight',
            'watermarkimg',
            'watermarkpct',
            'watermarkquality',
            'watermarkpos',
            'attachment_driver',
            'attachment_aliyun_key_id',
            'attachment_aliyun_key_secret',
            'attachment_aliyun_endpoint',
            'attachment_aliyun_bucket',
        ];
        $config = ConfigService::getConfig($fields)['data'];
        return View::fetch('setting',[
            'config' => $config
        ]);
    }

    /**
     * 上传实例
     *
     * @Author tanruizheng
     * @DateTime 2021-07-19
     * @return void
     */
    public function demo(){
        return View::fetch('demo');
    }
}
